package com.mobilesales.android.activities.clients;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.ModelActivity;
import com.mobilesales.android.adapters.SlidingMenus;
import com.mobilesales.android.database.ClientManager;
import com.mobilesales.android.model.clients.Client;
import com.mobilesales.android.model.dictionary.Dictionary;
import com.mobilesales.android.model.params.FieldType;
import com.mobilesales.android.model.params.Param;
import com.mobilesales.android.util.Constants;
import com.mobilesales.android.util.MessageBox;

public class DetailsClient extends ModelActivity<Client> {

	protected TextView nameTxt;
	protected TextView name2Txt;
	protected TextView groupTxt;
	protected TextView nipTxt;
	protected TextView streetTxt;
	protected TextView zipcodeTxt;
	protected TextView cityTxt;
	protected TextView phoneTxt;
	protected TextView faxTxt;
	protected TextView infoTxt;
	protected TextView info2Txt;
	protected TextView debtsTxt;
	protected TextView isSupplierTxt;

	private int clientId = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.client_details);

		new SlidingMenus(this);
		nameTxt = (TextView) this.findViewById(R.id.clientNameTxt);
		name2Txt = (TextView) this.findViewById(R.id.clientName2Txt);

		// groupTxt = (TextView) this.findViewById(R.id.groupTxt);

		nipTxt = (TextView) this.findViewById(R.id.clientNipTxt);
		streetTxt = (TextView) this.findViewById(R.id.clientStreetTxt);
		zipcodeTxt = (TextView) this.findViewById(R.id.clientZipcodeTxt);
		cityTxt = (TextView) this.findViewById(R.id.clientCityTxt);
		phoneTxt = (TextView) this.findViewById(R.id.clientPhoneTxt);
		faxTxt = (TextView) this.findViewById(R.id.clientFaxTxt);
		infoTxt = (TextView) this.findViewById(R.id.clientInfoTxt);
		info2Txt = (TextView) this.findViewById(R.id.clientInfo2Txt);
		debtsTxt = (TextView) this.findViewById(R.id.clientDebtsTxt);
		isSupplierTxt = (TextView) this.findViewById(R.id.isSupplierTxt);

		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null && bundle.containsKey(Constants.ClientId)) {
			clientId = bundle.getInt(Constants.ClientId);
			loadItem(clientId);
		} else {
			MessageBox.show(this, getString(R.string.ErrorClient));
		}

	}

	private void loadItem(int clientId) {
		Client client = getClient(clientId);
		if (client != null) {
			this.setModel(client);
		} else {
			MessageBox.show(this, getString(R.string.ErrorClient));
			finish();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		refresh();
	}

	@Override
	protected void setModel(Client client) {
		super.setModel(client);
		cityTxt.setText(client.City);
		nameTxt.setText(client.Name);
		name2Txt.setText(client.Name2);

		// groupTxt.setText(client.Group.Name);

		nipTxt.setText(client.Nip);
		streetTxt.setText(client.Street);
		zipcodeTxt.setText(client.Zipcode);
		cityTxt.setText(client.City);
		phoneTxt.setText(client.Phone);
		faxTxt.setText(client.Fax);
		infoTxt.setText(client.Info);
		info2Txt.setText(client.Info2);
		debtsTxt.setText(String.valueOf(client.Debts));
		isSupplierTxt.setText(getString(client.IsSupplier ? R.string.Yes
				: R.string.No));
		this.drawParams(client.Params);
	}

	/**
	 * 
	 * @param params
	 */
	private void drawParams(Dictionary<Param, String> params) {
		TableLayout table = (TableLayout) this.findViewById(R.id.fields);
		if (params != null) {
			List<Param> keys = params.getKeys();
			for (Param param : keys) {

				TableRow tr = new TableRow(this);
				LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				llp.setMargins(5, 5, 5, 5); // llp.setMargins(left, top, right,
											// bottom);
				tr.setLayoutParams(llp);
				TextView label = new TextView(this);
				llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				llp.setMargins(0, 0, 15, 0); // llp.setMargins(left, top, right,
												// bottom);
				label.setLayoutParams(llp);

				label.setText(param.DisplayName);
				TextView txt = new TextView(this);

				String val = params.getValue(param);

				if (val == null || val == "") {
					val = param.DefaultValue != null ? param.DefaultValue : "";
				}
				if (param.FieldType == FieldType.Boolean) {
					boolean b = Boolean.parseBoolean(val.toLowerCase());
					txt.setText(b ? getString(R.string.Yes)
							: getString(R.string.No));
				} else if (param.FieldType == FieldType.Dictionary
						&& params.getValue(param) == null) {
					String[] splitted = val.split(";");
					txt.setText(splitted[0]);
				} else {
					txt.setText(val);
				}
				tr.addView(label);
				tr.addView(txt);
				table.addView(tr);
			}
		}
		table.invalidate();

	}

	private Client getClient(int clientId) {
		ClientManager manager = new ClientManager(this);
		Client result = manager.GetClient(clientId);
		manager.Close();
		return result;
	}

	public void edit(View v) {
		Intent intent = new Intent(this, StoreClient.class);
		intent.putExtra(Constants.ClientId, getModel().Id);
		this.startActivity(intent);
	}

	public void refresh() {
		this.getHandler().post(new Runnable() {
			public void run() {
				loadItem(clientId);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuinf = new MenuInflater(this);
		menuinf.inflate(R.menu.client_details_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int i = item.getItemId();
		switch (i) {
		case R.id.menu_client_details_edit:
			edit(null);
			break;
		case R.id.menu_client_details_back:
			finish();
			break;
		default:
			break;
		}
		return true;
	}

}
